#include <QDebug>
#include <QSettings>
#include "strategyThread.h"
#include "KgGlobalData.h"
#include <math.h>

StrategyThread::StrategyThread(QObject *parent, CRTDB *pRTDB): QThread(parent),m_pRTDB(pRTDB)
{
    m_strLog = QString("../log/Strategy.log");
    devConfig();  
    KG_LOG_INFO(QString("VER1.0 RTDB%1.%2.%3").arg(BAMS_VERSION_MAJOR).arg(BAMS_VERSION_MINOR).arg(BAMS_VERSION_REVISION));
    m_pBattStack = m_pRTDB->getStack(0);
    initializeRangStruct();
	m_fNLYgLast = 0;
	m_fXLYgLast = 0;
    XLUpDelay = QDateTime::currentDateTime();
    XLDownDelay = QDateTime::currentDateTime();
    NLUpDelay = QDateTime::currentDateTime();
    NLDownDelay[0] = QDateTime::currentDateTime();
    NLDownDelay[1] = QDateTime::currentDateTime();
}

StrategyThread::~StrategyThread()
{}

void StrategyThread::run()
{
    while(1)
    {
        QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
        switch(set.value(QString("PCS/Strategy"), 0).toUInt())
        {
            case 1: funcPcs1(); break;
        }
        for (int index=0; (index<MAX_BANK_NUM)&&(MAX_AC_NUM); index++)
        {
            CAcData *pAcData = m_pRTDB->getAcDev(index);    
            QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
            switch(set.value(QString("AC-%1/Strategy").arg(index+1), 0).toUInt())
            {
                case 1: funcAc1(pAcData, index); break; // 下发温度值
                case 2: funcAc2(pAcData, index); break; // 002003913-内蒙中电-开关机
            }
        }        
        for (int index=0; (index<MAX_BANK_NUM)&&(MAX_LC_NUM); index++)
        {
            CLcData *pLcData = m_pRTDB->getLcDev(index);
            QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
            switch(set.value(QString("LC-%1/Strategy").arg(index+1), 0).toUInt())
            {
                case 1: funcLc1(pLcData, index); break; //【BMS-TMS控制策略】
            }
        }
        for (int index=0; (index<MAX_BANK_NUM)&&(MAX_DH_NUM); index++)
        {
            CDhData *pDhData = m_pRTDB->getDhDev(index);    
            QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
            switch(set.value(QString("DH-%1/Strategy").arg(index+1), 0).toUInt())
            {
                case 1: funcDh1(pDhData, index); break; // 下发温度值
            }
        }                
        msleep(1000);        
    }
}

void StrategyThread::funcAc1(CAcData *pAcData, int index)
{
    QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
    if ((set.value(QString("AC-%1/TempType").arg(index+1), 0).toUInt()) == 0)
    {
        tempf = 0;
        temp16 = 0;
        for (int i=0; i<m_pBattStack->usInstallBankNum; i++)
        {
            CBattBank *pBattBank = m_pRTDB->getBank(i);
            if ((pBattBank->usSglMaxTempNo != 999) && (pBattBank->usSglMinTempNo != 999))
            {
                tempf += pBattBank->fSglAvgTemp;
                temp16++;
            }
        }
        if (temp16 > 0) 
        {
            tempf /= temp16;
            tempf = round(tempf*10)/10.0; // 四舍五入保留一位小数
        }
        else tempf = 999;
        m_fMaxTemp = round(m_pBattStack->fSglMaxTempInStack*10)/10.0;
        m_fMinTemp = round(m_pBattStack->fSglMinTempInStack*10)/10.0;
        m_fAvgTemp = tempf;
    }
    else
    {
        CBattBank *pBattBank = m_pRTDB->getBank(index);
        m_fMaxTemp = round(pBattBank->fSglMaxTemp*10)/10.0;
        m_fMinTemp = round(pBattBank->fSglMinTemp*10)/10.0;
        m_fAvgTemp = round(pBattBank->fSglAvgTemp*10)/10.0;
    }
    if ((m_fMaxTemp == 999) || (m_fMinTemp == 999) || (m_fAvgTemp == 999))  
    {
        KG_LOG_INFO(QString("空调%1 温度无效 最大=%2 最小=%3 平均=%4").arg(index+1).arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp));
        pAcData->m_fRev[99] = FLT_MAX;
        pAcData->m_fRev[98] = FLT_MAX;
        pAcData->m_fRev[97] = FLT_MAX;
        pAcData->m_fRev[96] = FLT_MAX;
    }
    else
    {
        KG_LOG_INFO(QString("空调%1 温度 最大=%2 最小=%3 平均=%4").arg(index+1).arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp));
        pAcData->m_fRev[99] = m_fMaxTemp;
        pAcData->m_fRev[98] = m_fMinTemp;
        pAcData->m_fRev[97] = m_fAvgTemp;
        if (m_fMaxTemp > 30) tempf = m_fMaxTemp;
        else if (m_fMinTemp < 10) tempf = m_fMinTemp;
        else tempf = m_fAvgTemp;
        pAcData->m_fRev[96] = tempf;
    }
}

void StrategyThread::funcAc2(CAcData *pAcData, int index)
{
    QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
    if ((set.value(QString("AC-%1/TempType").arg(index+1), 0).toUInt()) == 0)
    {
        tempf = 0;
        temp16 = 0;
        for (int i=0; i<m_pBattStack->usInstallBankNum; i++)
        {
            CBattBank *pBattBank = m_pRTDB->getBank(i);
            if ((pBattBank->usSglMaxTempNo != 999) && (pBattBank->usSglMinTempNo != 999))
            {
                tempf += pBattBank->fSglAvgTemp;
                temp16++;
            }
        }
        if (temp16 > 0) 
        {
            tempf /= temp16;
            tempf = round(tempf*10)/10.0; // 四舍五入保留一位小数
        }
        else tempf = 999;
        m_fMaxTemp = round(m_pBattStack->fSglMaxTempInStack*10)/10.0;
        m_fMinTemp = round(m_pBattStack->fSglMinTempInStack*10)/10.0;
        m_fAvgTemp = tempf;
    }
    else
    {
        CBattBank *pBattBank = m_pRTDB->getBank(index);
        m_fMaxTemp = round(pBattBank->fSglMaxTemp*10)/10.0;
        m_fMinTemp = round(pBattBank->fSglMinTemp*10)/10.0;
        m_fAvgTemp = round(pBattBank->fSglAvgTemp*10)/10.0;
    }
    if ((m_fMaxTemp == 999) || (m_fMinTemp == 999) || (m_fAvgTemp == 999))  
    {
        KG_LOG_INFO(QString("空调%1 温度无效 最大=%2 最小=%3 平均=%4").arg(index+1).arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp));
        pAcData->m_fRev[99] = FLT_MAX;
        pAcData->m_fRev[98] = FLT_MAX;
        pAcData->m_fRev[97] = FLT_MAX;
        pAcData->m_fRev[96] = FLT_MAX;
        pAcData->m_uRev[98] = 0;
        pAcData->m_uRev[97] = 0;
        pAcData->m_uRev[96] = 0;
        pAcData->m_uRev[95] = 0;
    }
    else
    {
        KG_LOG_INFO(QString("空调%1 温度 最大=%2 最小=%3 平均=%4").arg(index+1).arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp));
        pAcData->m_fRev[99] = m_fMaxTemp;
        pAcData->m_fRev[98] = m_fMinTemp;
        pAcData->m_fRev[97] = m_fAvgTemp;
        if (m_fMaxTemp > 30) tempf = m_fMaxTemp;
        else if (m_fMinTemp < 10) tempf = m_fMinTemp;
        else tempf = m_fAvgTemp;
        pAcData->m_fRev[96] = tempf;
        // 策略
        float TmaxLev1 = set.value(QString("AC-%1/TmaxLev1").arg(index+1), 38).toFloat();
        float TmaxLev1Rst = set.value(QString("AC-%1/TmaxLev1Rst").arg(index+1), 33).toFloat();
        float TmaxLev2 = set.value(QString("AC-%1/TmaxLev2").arg(index+1), 35).toFloat();
        float TmaxLev2Rst = set.value(QString("AC-%1/TmaxLev2Rst").arg(index+1), 30).toFloat();
        float TminLev1 = set.value(QString("AC-%1/TminLev1").arg(index+1), -15).toFloat();
        float TminLev1Rst = set.value(QString("AC-%1/TminLev1Rst").arg(index+1), -10).toFloat();
        float HumiLev1 = set.value(QString("AC-%1/HumiLev1").arg(index+1), 80).toFloat();
        float HumiLev1Rst = set.value(QString("AC-%1/HumiLev1Rst").arg(index+1), 70).toFloat();
        // 最大温度所处状态           
        switch (pAcData->m_uRev[98])
        {
            case 1:
            {
                if (m_fMaxTemp <= TmaxLev1Rst)
                {
                    KG_LOG_INFO(QString("(高温一级>>高温正常) [Tmax_%1]≤%2").arg(m_fMaxTemp).arg(TmaxLev1Rst));
                    pAcData->m_uRev[98] = 0;
                }
                else
                {
                    KG_LOG_INFO(QString("(高温一级>>高温一级) Tmax=%1 TmaxLev1Rst=%2").arg(m_fMaxTemp).arg(TmaxLev1Rst));
                }
            }
            break;
            case 2:
            {
                if (m_fMaxTemp >= TmaxLev1)
                {
                    KG_LOG_INFO(QString("(高温二级>>高温一级) [Tmax_%1]≥%2").arg(m_fMaxTemp).arg(TmaxLev1));
                    pAcData->m_uRev[98] = 1;
                }
                else if (m_fMaxTemp <= TmaxLev2Rst)
                {
                    KG_LOG_INFO(QString("(高温二级>>高温正常) [Tmax_%1]≤%2").arg(m_fMaxTemp).arg(TmaxLev2Rst));
                    pAcData->m_uRev[98] = 0;
                }
                else
                {
                    KG_LOG_INFO(QString("(高温二级>>高温二级) Tmax=%1 TmaxLev2Rst=%2").arg(m_fMaxTemp).arg(TmaxLev2Rst));
                }
            }
            break;
            default: 
            {
                if (m_fMaxTemp >= TmaxLev1)
                {
                    KG_LOG_INFO(QString("(高温正常>>高温一级) [Tmax_%1]≥%2").arg(m_fMaxTemp).arg(TmaxLev1));
                    pAcData->m_uRev[98] = 1;
                }
                else if (m_fMaxTemp >= TmaxLev2)
                {
                    KG_LOG_INFO(QString("(高温正常>>高温二级) [Tmax_%1]≥%2").arg(m_fMaxTemp).arg(TmaxLev2));
                    pAcData->m_uRev[98] = 2;
                }
                else
                {
                    KG_LOG_INFO(QString("(高温正常>>高温正常) Tmax=%1 TmaxLev1=%2 TmaxLev2=%3").arg(m_fMaxTemp).arg(TmaxLev1).arg(TmaxLev2));
                    pAcData->m_uRev[98] = 0;
                }
            }
            break;
        }
        // 最小温度所处状态
        switch (pAcData->m_uRev[97])
        {
            case 1:
            {
                if (m_fMinTemp >= TminLev1Rst)
                {
                    KG_LOG_INFO(QString("(低温一级>>低温正常) [Tmin_%1]≥%2").arg(m_fMinTemp).arg(TminLev1Rst));
                    pAcData->m_uRev[97] = 0;
                }
                else
                {
                    KG_LOG_INFO(QString("(低温一级>>低温一级) Tmin=%1 TminLev1Rst=%2").arg(m_fMinTemp).arg(TminLev1Rst));
                }
            }
            break;
            default:
            {
                if (m_fMinTemp <= TminLev1)
                {
                    KG_LOG_INFO(QString("(低温正常>>低温一级) [Tmin_%1]≤%2").arg(m_fMinTemp).arg(TminLev1));
                    pAcData->m_uRev[97] = 1;
                }
                else
                {
                    KG_LOG_INFO(QString("(低温正常>>低温正常) Tmin=%1 TminLev1=%2").arg(m_fMinTemp).arg(TminLev1));
                    pAcData->m_uRev[97] = 0;
                }
            }
            break;
        }
        // 湿度所处状态
        switch (pAcData->m_uRev[96])
        {
            case 1:
            {
                if ((pAcData->m_fRev[95] <= HumiLev1Rst) && (pAcData->m_fRev[95] != FLT_MAX))
                {
                    KG_LOG_INFO(QString("(湿度一级>>湿度正常) [Humi_%1]≤%2").arg(pAcData->m_fRev[95]).arg(HumiLev1Rst));
                    pAcData->m_uRev[96] = 0;
                }
                else
                {
                    KG_LOG_INFO(QString("(湿度一级>>湿度一级) Humi=%1 HumiLev1Rst=%2").arg(pAcData->m_fRev[95]).arg(HumiLev1Rst));
                }
            }
            break;
            default:
            {
                if ((pAcData->m_fRev[95] >= HumiLev1) && (pAcData->m_fRev[95] != FLT_MAX))
                {
                    KG_LOG_INFO(QString("(湿度正常>>湿度一级) [Humi_%1]≥%2").arg(pAcData->m_fRev[95]).arg(HumiLev1));
                    pAcData->m_uRev[96] = 1;
                }
                else
                {
                    KG_LOG_INFO(QString("(湿度正常>>湿度正常) Humi=%1 HumiLev1=%2").arg((pAcData->m_fRev[95]!=FLT_MAX)?QString("%1").arg(pAcData->m_fRev[95]):QString("%1").arg("FLT_MAX")).arg(HumiLev1));
                    pAcData->m_uRev[96] = 0;
                }
            }
            break;
        }
        // 下发开关机
        pAcData->m_uRev[95] = (pAcData->m_uRev[98] || pAcData->m_uRev[97] || pAcData->m_uRev[96])?1:0;
    }
}

void StrategyThread::funcDh1(CDhData *pDhData, int index)
{
    QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
    if ((set.value(QString("AC-%1/TempType").arg(index+1), 0).toUInt()) == 0)
    {
        tempf = 0;
        temp16 = 0;
        for (int i=0; i<m_pBattStack->usInstallBankNum; i++)
        {
            CBattBank *pBattBank = m_pRTDB->getBank(i);
            if ((pBattBank->usSglMaxTempNo != 999) && (pBattBank->usSglMinTempNo != 999))
            {
                tempf += pBattBank->fSglAvgTemp;
                temp16++;
            }
        }
        if (temp16 > 0) 
        {
            tempf /= temp16;
            tempf = round(tempf*10)/10.0; // 四舍五入保留一位小数
        }
        else tempf = 999;
        m_fMaxTemp = round(m_pBattStack->fSglMaxTempInStack*10)/10.0;
        m_fMinTemp = round(m_pBattStack->fSglMinTempInStack*10)/10.0;
        m_fAvgTemp = tempf;
    }
    else
    {
        CBattBank *pBattBank = m_pRTDB->getBank(index);
        m_fMaxTemp = round(pBattBank->fSglMaxTemp*10)/10.0;
        m_fMinTemp = round(pBattBank->fSglMinTemp*10)/10.0;
        m_fAvgTemp = round(pBattBank->fSglAvgTemp*10)/10.0;
    }
    if ((m_fMaxTemp == 999) || (m_fMinTemp == 999) || (m_fAvgTemp == 999))  
    {
        KG_LOG_INFO(QString("除湿%1 温度无效 最大=%2 最小=%3 平均=%4").arg(index+1).arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp));
        pDhData->m_fRev[99] = FLT_MAX;
        pDhData->m_fRev[98] = FLT_MAX;
        pDhData->m_fRev[97] = FLT_MAX;
        pDhData->m_fRev[96] = FLT_MAX;
    }
    else
    {
        KG_LOG_INFO(QString("除湿%1 温度 最大=%2 最小=%3 平均=%4").arg(index+1).arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp));
        pDhData->m_fRev[99] = m_fMaxTemp;
        pDhData->m_fRev[98] = m_fMinTemp;
        pDhData->m_fRev[97] = m_fAvgTemp;
        if (m_fMaxTemp > 30) tempf = m_fMaxTemp;
        else if (m_fMinTemp < 10) tempf = m_fMinTemp;
        else tempf = m_fAvgTemp;
        pDhData->m_fRev[96] = tempf;
    }
}

void StrategyThread::funcLc1(CLcData *pLcData, int index)
{
    QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
    // 一级制冷 28≤Tmax<35 且 25≤Tvag (TMS回水温度≤12 不能开启制冷)
    m_fSglTempColdDownValLev1 = set.value(QString("LC-%1/m_fSglTempColdDownValLev1").arg(index+1), 28).toFloat();
    m_fSglTempColdUpValLev1 = set.value(QString("LC-%1/m_fSglTempColdUpValLev1").arg(index+1), 35).toFloat();
    m_fAvgTempColdDownValLev1 = set.value(QString("LC-%1/m_fAvgTempColdDownValLev1").arg(index+1), 25).toFloat();
    // 二级制冷 35≤Tmax 且 25≤Tvag (TMS回水温度≤12 不能开启制冷)
    m_fSglTempColdDownValLev2 = set.value(QString("LC-%1/m_fSglTempColdDownValLev2").arg(index+1), 35).toFloat();
    m_fAvgTempColdDownValLev2 = set.value(QString("LC-%1/m_fAvgTempColdDownValLev2").arg(index+1), 25).toFloat();
    // 制冷截至条件 Tmax≤25 或 Tvag≤22
    m_fSglTempStopColdUpVal = set.value(QString("LC-%1/m_fSglTempStopColdUpVal").arg(index+1), 25).toFloat();
    m_fAvgTempStopColdUpVal = set.value(QString("LC-%1/m_fAvgTempStopColdUpVal").arg(index+1), 22).toFloat();
    // 制热条件 Tmin≤12 且 Tvag≤15 (TMS回水温度≤12 可以开启制热)
    m_fSglTempHeatUpVal = set.value(QString("LC-%1/m_fSglTempHeatUpVal").arg(index+1), 12).toFloat();
    m_fAvgTempHeatUpVal = set.value(QString("LC-%1/m_fAvgTempHeatUpVal").arg(index+1), 15).toFloat();
    // 制热截至条件 20≤Tmin 或 23≤Tvag
    m_fSglTempStopHeatDownVal = set.value(QString("LC-%1/m_fSglTempStopHeatDownVal").arg(index+1), 20).toFloat();
    m_fAvgTempStopHeatDownVal = set.value(QString("LC-%1/m_fAvgTempStopHeatDownVal").arg(index+1), 23).toFloat();
    // 自循环条件 25≤Tmax<28 且 22≤Tvag<25, 或 TMS回水温度≤12
    m_fSglTempLoopDownVal = set.value(QString("LC-%1/m_fSglTempLoopDownVal").arg(index+1), 25).toFloat();
    m_fSglTempLoopUpVal = set.value(QString("LC-%1/m_fSglTempLoopUpVal").arg(index+1), 28).toFloat();
    m_fAvgTempLoopDownVal = set.value(QString("LC-%1/m_fAvgTempLoopDownVal").arg(index+1), 22).toFloat();
    m_fAvgTempLoopUpVal = set.value(QString("LC-%1/m_fAvgTempLoopUpVal").arg(index+1), 25).toFloat();
    m_fTmsHuiShuiTempLoopUpVal = set.value(QString("LC-%1/m_fTmsHuiShuiTempLoopUpVal").arg(index+1), 12).toFloat();
    // 待机模式下 6≤温差 进入自循环, 自循环模式下 12<TMS回水温度且电池温差≤3(无故障) 进入待机
    m_fSglTempDiffLoopDownVal = set.value(QString("LC-%1/m_fSglTempDiffLoopDownVal").arg(index+1), 6).toFloat();
    m_fSglTempDiffLoopUpVal = set.value(QString("LC-%1/m_fSglTempDiffLoopUpVal").arg(index+1), 3).toFloat();
    // 自循环截至条件 Tmax<25 或 28≤Tmax
    m_fSglTempStopLoopUpVal = set.value(QString("LC-%1/m_fSglTempStopLoopUpVal").arg(index+1), 25).toFloat();
    m_fSglTempStopLoopDownVal = set.value(QString("LC-%1/m_fSglTempStopLoopDownVal").arg(index+1), 28).toFloat(); 
    // 一级制冷的设定温度
    m_fCoolTempLev1 = set.value(QString("LC-%1/m_fCoolTempLev1").arg(index+1), 20).toFloat();
    // 二级制冷的设定温度
    m_fCoolTempLev2 = set.value(QString("LC-%1/m_fCoolTempLev2").arg(index+1), 15).toFloat();
    // 制热的设定温度
    m_fHeatTemp = set.value(QString("LC-%1/m_fHeatTemp").arg(index+1), 30).toFloat();
    // 待机的设定温度
    m_fStopTemp = set.value(QString("LC-%1/m_fStopTemp").arg(index+1), 25).toFloat();
    // 温度类型 0堆温度 >0组温度
    if ((set.value(QString("LC-%1/TempType").arg(index+1), 0).toUInt()) == 0)
    {
        tempf = 0;
        temp16 = 0;
        for (int i=0; i<m_pBattStack->usInstallBankNum; i++)
        {
            CBattBank *pBattBank = m_pRTDB->getBank(i);
            if ((pBattBank->usSglMaxTempNo != 999) && (pBattBank->usSglMinTempNo != 999))
            {
                tempf += pBattBank->fSglAvgTemp;
                temp16++;
            }
        }
        if (temp16 > 0) 
        {
            tempf /= temp16;
            tempf = round(tempf*10)/10.0; // 四舍五入保留一位小数
        }
        else tempf = 999;
        m_fMaxTemp = round(m_pBattStack->fSglMaxTempInStack*10)/10.0;
        m_fMinTemp = round(m_pBattStack->fSglMinTempInStack*10)/10.0;
        m_fAvgTemp = tempf;
        m_fDifTemp = round(m_pBattStack->fSglTempDiffInStack*10)/10.0;

        if (m_pBattStack->fStackCur > 1) temp16 = 1;
        else if (m_pBattStack->fStackCur < -1) temp16 = 2;
        else temp16 = 0;
        pLcData->m_uRev[96] = temp16;
    }
    else
    {
        CBattBank *pBattBank = m_pRTDB->getBank(index);
        m_fMaxTemp = round(pBattBank->fSglMaxTemp*10)/10.0;
        m_fMinTemp = round(pBattBank->fSglMinTemp*10)/10.0;
        m_fAvgTemp = round(pBattBank->fSglAvgTemp*10)/10.0;
        m_fDifTemp = round(pBattBank->fSglTempDiffInBank*10)/10.0;     
        
        if (pBattBank->fBankCur > 1) temp16 = 1;
        else if (pBattBank->fBankCur < -1) temp16 = 2;
        else temp16 = 0;
        pLcData->m_uRev[96] = temp16;
    }

    if ((m_fMaxTemp == 999) || (m_fMinTemp == 999) || (m_fAvgTemp == 999) || (m_fDifTemp == 999))  
    {
        pLcData->m_fRev[97] = FLT_MAX;
        pLcData->m_fRev[96] = FLT_MAX;
        pLcData->m_fRev[95] = FLT_MAX;
        pLcData->m_fRev[94] = FLT_MAX;
    }
    else     
    {
        pLcData->m_fRev[97] = m_fMaxTemp;
        pLcData->m_fRev[96] = m_fMinTemp;
        pLcData->m_fRev[95] = m_fAvgTemp;
        if (m_fMaxTemp > 30) tempf = m_fMaxTemp;
        else if (m_fMinTemp < 10) tempf = m_fMinTemp;
        else tempf = m_fAvgTemp;
        pLcData->m_fRev[94] = tempf;
    }

    if ((m_fMaxTemp == 999) || (m_fMinTemp == 999) || (m_fAvgTemp == 999) || (m_fDifTemp == 999))  
    {
        KG_LOG_INFO(QString("液冷%1 停止运行(温度无效 最大=%2 最小=%3 平均=%4 温差=%5)").arg(index+1).arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp).arg(m_fDifTemp));
        pLcData->m_uRev[97] = 0;
        pLcData->m_fRev[98] = m_fStopTemp;      
    }
    else if ((m_pBattStack->aEmerStop != 0) || (m_pBattStack->aWaterAlarm != 0) || (m_pBattStack->aFireAction != 0))
    {
        KG_LOG_INFO(QString("液冷%1 停止运行(急停=%2 水浸=%3 消防=%4)").arg(index+1).arg((quint8)m_pBattStack->aEmerStop).arg((quint8)m_pBattStack->aWaterAlarm).arg((quint8)m_pBattStack->aFireAction));
        pLcData->m_uRev[97] = 0;
        pLcData->m_fRev[98] = m_fStopTemp;
    }
    else if (pLcData->m_uCommState != 0)
    {
        KG_LOG_INFO(QString("液冷%1 停止运行(通讯状态=%2)").arg(index+1).arg(pLcData->m_uCommState, 0, 16).toUpper());
        pLcData->m_uRev[97] = 0;
        pLcData->m_fRev[98] = m_fStopTemp;
    }
    else
    {
        switch (pLcData->m_uRev[97])
        {
            case 11:
            case 12: getTmsStateOnCool(pLcData); break;    // 制冷
            case 2:  getTmsStateOnHeat(pLcData); break;    // 制热
            case 3:  getTmsStateOnLoop(pLcData); break;    // 自循环
            default: getTmsStateOnStandby(pLcData); break; // 待机
        }
        switch (pLcData->m_uRev[97])
        {
            case 11: pLcData->m_fRev[98] = m_fCoolTempLev1; break; // 1级制冷
            case 12: pLcData->m_fRev[98] = m_fCoolTempLev2; break; // 2级制冷
            case 2:  pLcData->m_fRev[98] = m_fHeatTemp; break;     // 制热
            default: pLcData->m_fRev[98] = m_fStopTemp;   
        }    
    }
}

void StrategyThread::getTmsStateOnStandby(CLcData *pLcData)
{
    if ((m_fMaxTemp >= m_fSglTempColdDownValLev1) && (m_fMaxTemp < m_fSglTempColdUpValLev1) && (m_fAvgTemp >= m_fAvgTempColdDownValLev1) && (pLcData->m_fRev[99] > m_fTmsHuiShuiTempLoopUpVal))
    {
        KG_LOG_INFO(QString("(待机>>一级制冷) %1≤[Tmax_%2]<%3且[Tvag_%4]≥%5且[回水温度%6]>%7").arg(m_fSglTempColdDownValLev1).arg(m_fMaxTemp).arg(m_fSglTempColdUpValLev1)\
            .arg(m_fAvgTemp).arg(m_fAvgTempColdDownValLev1).arg(pLcData->m_fRev[99]).arg(m_fTmsHuiShuiTempLoopUpVal));
        pLcData->m_uRev[97] = 11;
    }
    else if ((m_fMaxTemp >= m_fSglTempColdDownValLev2) && (m_fAvgTemp >= m_fAvgTempColdDownValLev2) && (pLcData->m_fRev[99] > m_fTmsHuiShuiTempLoopUpVal))
    {
        KG_LOG_INFO(QString("(待机>>二级制冷) [Tmax_%1]≥%2且[Tvag_%3]≥%4且[回水温度%5]>%6").arg(m_fMaxTemp).arg(m_fSglTempColdDownValLev2).arg(m_fAvgTemp)\
            .arg(m_fAvgTempColdDownValLev2).arg(pLcData->m_fRev[99]).arg(m_fTmsHuiShuiTempLoopUpVal));
        pLcData->m_uRev[97] = 12;
    }    
    else if ((m_fMinTemp <= m_fSglTempHeatUpVal) && (m_fAvgTemp <= m_fAvgTempHeatUpVal))
    {
        KG_LOG_INFO(QString("(待机>>制热) [Tmin_%1]≤%2且[Tvag_%3]≤%4").arg(m_fMinTemp).arg(m_fSglTempHeatUpVal).arg(m_fAvgTemp).arg(m_fAvgTempHeatUpVal));
        pLcData->m_uRev[97] = 2;
    }
    else if (((m_fMaxTemp >= m_fSglTempLoopDownVal) && (m_fMaxTemp < m_fSglTempLoopUpVal) && (m_fAvgTemp >= m_fAvgTempLoopDownVal) && (m_fAvgTemp < m_fAvgTempLoopUpVal)
    && (!((pLcData->m_fRev[99] > m_fTmsHuiShuiTempLoopUpVal) && (pLcData->m_uRev[98] == 0) && (m_fDifTemp <= m_fSglTempDiffLoopUpVal)))) // 自循环>>待机 优先待机
    || (m_fDifTemp >= m_fSglTempDiffLoopDownVal))
    {
        KG_LOG_INFO(QString("(待机>>自循环) (%1≤[Tmax_%2]<%3且%4≤[Tvag_%5]<%6)或[温差%7]≥%8").arg(m_fSglTempLoopDownVal).arg(m_fMaxTemp).arg(m_fSglTempLoopUpVal)\
            .arg(m_fAvgTempLoopDownVal).arg(m_fAvgTemp).arg(m_fAvgTempLoopUpVal).arg(m_fDifTemp).arg(m_fSglTempDiffLoopDownVal));
        pLcData->m_uRev[97] = 3;
    }
    else
    {
        KG_LOG_INFO(QString("(待机>>待机) 最大温度=%1 最小温度=%2 平均温度=%3 温差=%4 回水温度=%5").arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp).arg(m_fDifTemp).arg(pLcData->m_fRev[99]));
        pLcData->m_uRev[97] = 0;
    }
}

void StrategyThread::getTmsStateOnCool(CLcData *pLcData)
{
    if ((m_fMaxTemp <= m_fSglTempStopColdUpVal) || (m_fAvgTemp <= m_fAvgTempStopColdUpVal) || (pLcData->m_fRev[99] <= m_fTmsHuiShuiTempLoopUpVal))
    {
        KG_LOG_INFO(QString("(%1制冷>>待机) [Tmax_%2]≤%3或[Tvag_%4]≤%5或[回水温度%6]≤%7").arg(pLcData->m_uRev[97]==11?"一级":"二级").arg(m_fMaxTemp).arg(m_fSglTempStopColdUpVal)\
            .arg(m_fAvgTemp).arg(m_fAvgTempStopColdUpVal).arg(pLcData->m_fRev[99]).arg(m_fTmsHuiShuiTempLoopUpVal));
        getTmsStateOnStandby(pLcData);
    }
    else if ((m_fMaxTemp >= m_fSglTempColdDownValLev1) && (m_fMaxTemp < m_fSglTempColdUpValLev1) && (m_fAvgTemp >= m_fAvgTempColdDownValLev1))
    {
        KG_LOG_INFO(QString("(%1制冷>>一级制冷) %2≤[Tmax_%3]<%4且[Tvag_%5]≥%6").arg(pLcData->m_uRev[97]==11?"一级":"二级").arg(m_fSglTempColdDownValLev1).arg(m_fMaxTemp)\
            .arg(m_fSglTempColdUpValLev1).arg(m_fAvgTemp).arg(m_fAvgTempColdDownValLev1));
        pLcData->m_uRev[97] = 11;
    }
    else if ((m_fMaxTemp >= m_fSglTempColdDownValLev2) && (m_fAvgTemp >= m_fAvgTempColdDownValLev2))
    {
        KG_LOG_INFO(QString("(%1制冷>>二级制冷) [Tmax_%2]≥%3且[Tvag_%4]≥%5").arg(pLcData->m_uRev[97]==11?"一级":"二级").arg(m_fMaxTemp).arg(m_fSglTempColdDownValLev2)\
            .arg(m_fAvgTemp).arg(m_fAvgTempColdDownValLev2));
        pLcData->m_uRev[97] = 12;
    } 
    else 
    {
        KG_LOG_INFO(QString("(%1制冷>>%1制冷) 最大温度=%2 最小温度=%3 平均温度=%4 温差=%5 回水温度=%6").arg(pLcData->m_uRev[97]==11?"一级":"二级").arg(m_fMaxTemp).arg(m_fMinTemp)\
            .arg(m_fAvgTemp).arg(m_fDifTemp).arg(pLcData->m_fRev[99]));
        (void)pLcData->m_uRev[97];    
    }
}

void StrategyThread::getTmsStateOnHeat(CLcData *pLcData)
{
    if ((m_fMinTemp >= m_fSglTempStopHeatDownVal) || (m_fAvgTemp >= m_fAvgTempStopHeatDownVal))
    {
        KG_LOG_INFO(QString("(制热>>待机) [Tmin]%1≥%2或[Tvag]%3≥%4").arg(m_fMinTemp).arg(m_fSglTempStopHeatDownVal).arg(m_fAvgTemp).arg(m_fAvgTempStopHeatDownVal));
        getTmsStateOnStandby(pLcData);
    }
    else
    {
        KG_LOG_INFO(QString("(制热>>制热) 最大温度=%1 最小温度=%2 平均温度=%3 温差=%4 回水温度=%5").arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp).arg(m_fDifTemp).arg(pLcData->m_fRev[99]));   
        pLcData->m_uRev[97] = 2;
    }
}

void StrategyThread::getTmsStateOnLoop(CLcData *pLcData)
{
    if ((m_fMaxTemp >= m_fSglTempColdDownValLev1) && (m_fMaxTemp < m_fSglTempColdUpValLev1) && (m_fAvgTemp >= m_fAvgTempColdDownValLev1) && (pLcData->m_fRev[99] > m_fTmsHuiShuiTempLoopUpVal))
    {
        KG_LOG_INFO(QString("(自循环>>一级制冷) %1≤[Tmax_%2]<%3且[Tvag_%4]≥%5且[回水温度%6]>%7").arg(m_fSglTempColdDownValLev1).arg(m_fMaxTemp).arg(m_fSglTempColdUpValLev1)\
            .arg(m_fAvgTemp).arg(m_fAvgTempColdDownValLev1).arg(pLcData->m_fRev[99]).arg(m_fTmsHuiShuiTempLoopUpVal));
        pLcData->m_uRev[97] = 11;
    }
    else if ((m_fMaxTemp >= m_fSglTempColdDownValLev2) && (m_fAvgTemp >= m_fAvgTempColdDownValLev2) && (pLcData->m_fRev[99] > m_fTmsHuiShuiTempLoopUpVal))
    {
        KG_LOG_INFO(QString("(自循环>>二级制冷) [Tmax_%1]≥%2且[Tvag_%3]≥%4且[回水温度%5]>%6").arg(m_fMaxTemp).arg(m_fSglTempColdDownValLev2).arg(m_fAvgTemp)\
            .arg(m_fAvgTempColdDownValLev2).arg(pLcData->m_fRev[99]).arg(m_fTmsHuiShuiTempLoopUpVal));
        pLcData->m_uRev[97] = 12;
    }    
    else if ((m_fMinTemp <= m_fSglTempHeatUpVal) && (m_fAvgTemp <= m_fAvgTempHeatUpVal))
    {
        KG_LOG_INFO(QString("(自循环>>制热) [Tmin_%1]≤%2且[Tvag_%3]≤%4").arg(m_fMinTemp).arg(m_fSglTempHeatUpVal).arg(m_fAvgTemp).arg(m_fAvgTempHeatUpVal));
        pLcData->m_uRev[97] = 2;
    }
    else
    {
        if ((pLcData->m_fRev[99] > m_fTmsHuiShuiTempLoopUpVal) && (pLcData->m_uRev[98] == 0) && (m_fDifTemp <= m_fSglTempDiffLoopUpVal))
        {   
            KG_LOG_INFO(QString("(自循环>>待机) [回水温度%1]>%2且液冷无故障且[温差%3]≤%4").arg(pLcData->m_fRev[99]).arg(m_fTmsHuiShuiTempLoopUpVal)\
                .arg(m_fDifTemp).arg(m_fSglTempDiffLoopUpVal));
            pLcData->m_uRev[97] = 0;
        }
        else if (((m_fMaxTemp < m_fSglTempStopLoopUpVal) || (m_fMaxTemp >= m_fSglTempStopLoopDownVal))
        && (!(m_fDifTemp >= m_fSglTempDiffLoopDownVal)))
        {
            KG_LOG_INFO(QString("(自循环>>待机) [Tmax_%1]<%2或[Tmax_%1]≥%3").arg(m_fMaxTemp).arg(m_fSglTempStopLoopUpVal).arg(m_fSglTempStopLoopDownVal));
            getTmsStateOnStandby(pLcData);
        }
        else
        {
            KG_LOG_INFO(QString("(自循环>>自循环) 最大温度=%1 最小温度=%2 平均温度=%3 温差=%4 回水温度=%5").arg(m_fMaxTemp).arg(m_fMinTemp).arg(m_fAvgTemp).arg(m_fDifTemp).arg(pLcData->m_fRev[99]));
            pLcData->m_uRev[97] = 3;
        }
    }
}

void StrategyThread::devConfig(void)
{
    QSettings set(DEV_PATH, QSettings::IniFormat);
    m_isOutputLog = set.value("Log/IsOutputLog", 0).toInt();
    if (m_isOutputLog == 1)
    {
        QDir dir;
        dir.mkdir("../log");
        QString   logFileName = QString(m_strLog) ;
        QFile file(logFileName);
        file.remove();
        m_logFile.createlogfile(logFileName);
    }
}

void StrategyThread::writeKgLogfile(QString strInfo,bool bSaveToFile)
{
    QByteArray dt = strInfo.toLocal8Bit();
    char *p = dt.data();
    printf(p);
    printf("\n");
    if (1 != m_isOutputLog)//|| !bSaveToFile
    {
        return;
    }
    if (!bSaveToFile)
    {
        return;
    }
    int fileSize = m_logFile.fileSize();
    if (fileSize >= KG_MAX_LOG_SIZE)
    {
        printf("log size:%d, too large to delete\n", fileSize);
        m_logFile.close();
        QString   logFileName = QString(m_strLog);
        QFile file(logFileName);
        file.remove();
        m_logFile.createlogfile(logFileName);
    }    
    m_logFile.writelogfile(strInfo);
}

void StrategyThread::funcPcs1(void)
{
    QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
    getLocalCurveYgPower();
    getLocalCurveWgPower();
    getRemoteCurveYgPower();
    getRemoteCurveWgPower();       
    getXLNL();
    KG_LOG_INFO(QString("/"));

    switch (m_pRTDB->getPcs(0)->m_uRunMode)
    {
        case 1: // 就地手动
        {
            m_fYgPower = 0;
            m_fWgPower = 0;
        }
        break;
        case 2: // 就地曲线
        {
            QDateTime currentDateTime = QDateTime::currentDateTime();
            int minute = currentDateTime.time().minute() + (currentDateTime.time().hour()*60);
            for (int i=0; i< KG_MAX_TIME_NUMBER; i++)
            {
                TimeSegment &ts = m_timeSegment[i];
                if ((minute >= ts.nStartMinute) && (minute < ts.nEndMinute))
                {
                    m_fYgPower = m_LocalCurveYgPower[i];
                    m_fWgPower = m_LocalCurveWgPower[i];             
                    break;
                }
            }
        }
        break;
        case 4: // 远程曲线
        {
            QDateTime currentDateTime = QDateTime::currentDateTime();
            int minute = currentDateTime.time().minute() + (currentDateTime.time().hour()*60);
            for (int i=0; i< KG_MAX_TIME_NUMBER; i++)
            {
                TimeSegment &ts = m_timeSegment[i];
                if ((minute >= ts.nStartMinute) && (minute < ts.nEndMinute))
                {
                    m_fYgPower = m_RemoteCurveYgPower[i];
                    m_fWgPower = m_RemoteCurveWgPower[i];     
                    break;
                }
            }
        }
        break;
        default:{
            m_fYgPower = 0;
            m_fWgPower = 0;
        }
        break;
    }
    KG_LOG_INFO(QString("1、下设方式=%1 有功功率=%2 无功功率=%3").arg(m_pRTDB->getPcs(0)->m_uRunMode).arg(m_fYgPower).arg(m_fWgPower));

    m_XLMeter = set.value("PCS/XLMeter", 1).toInt();
    m_NLMeter = set.value("PCS/NLMeter", 1).toInt();
    m_fXLYgPower = m_pRTDB->getMeter(m_XLMeter)->m_fTotalActPower;
    CMeterData *pNLMeter = m_pRTDB->getMeter(m_NLMeter);
    m_fNLYgPower[0] = pNLMeter->m_fTotalActPower;
    m_fNLYgPower[1] = (pNLMeter->m_fAPhaseActPower<pNLMeter->m_fBPhaseActPower)?pNLMeter->m_fAPhaseActPower:pNLMeter->m_fBPhaseActPower;
    m_fNLYgPower[1] = (m_fNLYgPower[1]<pNLMeter->m_fCPhaseActPower)?m_fNLYgPower[1]:pNLMeter->m_fCPhaseActPower;
    KG_LOG_INFO(QString("2、逆流功率=%1 %2 需量功率=%3 逆流表号=%4 需量表号=%5").arg(m_fNLYgPower[0]).arg(m_fNLYgPower[1]).arg(m_fXLYgPower).arg(m_NLMeter).arg(m_XLMeter));

    m_NLPoint = set.value("PCS/NLPoint", 0).toFloat();
    m_NLMode = set.value("PCS/NLMode", 0).toInt();
    if (m_fYgPower > 0)
    {
        if (m_bXL)
        {
            m_fYgPower = XL(m_fXLYgLast);
        }
    }
    else
    {
        m_fXLYgLast = 0;
    }
    if (m_fYgPower < 0)
    {
        if (m_bNL)
        {
            switch(m_NLMode)
            {
                case 1: m_fYgPower = NLFunc1(m_fNLYgLast); break; // 总有功或单相
                default:m_fYgPower = NLFunc0(m_fNLYgLast); break; // 仅总有功
            }
        }
    }
    else
    {
        m_fNLYgLast = 0;
    }
    KG_LOG_INFO(QString("3、有功计算=(%1) 逆流基准=%2 逆流模式=%3 逆流使能=%4 逆流余量=%5 需量使能=%6 需量余量=%7 本月需量=%8").arg(m_fYgPower).arg(m_NLPoint).arg(m_NLMode).arg(m_bNL).arg(m_NLmargin).arg(m_bXL).arg(m_XLmargin).arg(m_XLToday));

    m_fYgPower /= (float)m_pBattStack->usInstallBankNum; // 每组功率=有功计算/组数

    for (int index=0; index<m_pBattStack->usInstallBankNum; index++)
    {
        CPcsData *pPcsData = m_pRTDB->getPcs(index);
        CBattBank *pBattBank = m_pRTDB->getBank(index);
        int uCmdState = 0;
        QString str = NULL;
        if ((pBattBank->usBankState == 0xAAAA) || (pBattBank->usBankState == 0x5555))
        {
            uCmdState = PCS_STOP;
        }
        else if ((pBattBank->usBankState == 0x1111) && (m_fYgPower > 0))
        {
            uCmdState = PCS_STANDBY;
        }  
        else if ((pBattBank->usBankState == 0x2222) && (m_fYgPower < 0))
        {
            uCmdState = PCS_STANDBY;
        }
        else if (pPcsData->m_uFaultState)
        {
            uCmdState = PCS_RESET;
        }
        else if (pPcsData->m_uStopState)
        {
            uCmdState = PCS_START;
        }
        else if (m_fYgPower > 0)
        {
            uCmdState = PCS_CHARGE;
        }
        else if (m_fYgPower < 0)
        {
            uCmdState = PCS_DISCHARGE;
        }
        else
        {
            uCmdState = PCS_STANDBY;
        }
        switch (uCmdState)
        {
            case PCS_STANDBY:
            {
                pPcsData->m_fSetYgPower = 0;
                pPcsData->m_fSetWgPower = 0;
                if (fabs(pPcsData->m_fTotalActPower) < 2)
                {
                    pPcsData->m_uStandby = 1;
                }
                str = "待机";
            }
            break;
            case PCS_RESET:
            {
                pPcsData->m_uReset = 1;
                str = "复位";
            }
            break;
            case PCS_START:
            {
                pPcsData->m_uStart = 1;
                str = "启机";
            }
            break;
            case PCS_CHARGE:
            {
                float fChaPower = pBattBank->fBankAllowMaxChargePower;
                pPcsData->m_fSetYgPower = fChaPower<m_fYgPower?fChaPower:m_fYgPower;
                pPcsData->m_fSetWgPower = m_fWgPower;
                str = "充电";
            }
            break;
            case PCS_DISCHARGE:
            {
                float fDisPower = (-pBattBank->fBankAllowMaxDischargePower);
                pPcsData->m_fSetYgPower = fDisPower>m_fYgPower?fDisPower:m_fYgPower;
                pPcsData->m_fSetWgPower = m_fWgPower;
                str = "放电";
            }
            break;
            default:
            {
                pPcsData->m_fSetYgPower = 0;
                pPcsData->m_fSetWgPower = 0;
                pPcsData->m_uStop = 1;
                str = "停机";
            }
            break;
        }
        KG_LOG_INFO(QString("4、%1=%2 有功=%3 无功=%4 状态=%5 充限=%6 放限=%7").arg(index+1,2,10,QLatin1Char('0')).arg(str).arg(pPcsData->m_fSetYgPower).arg(pPcsData->m_fSetWgPower).arg(pBattBank->usBankState,0,16).toUpper().arg(pBattBank->fBankAllowMaxChargePower).arg(pBattBank->fBankAllowMaxDischargePower));
    }
}

float StrategyThread::XL(float &fYgLast)
{
    static float fYg = 0, fYgPowerLast = 0;

    if (fYgPowerLast != m_fYgPower)
    {
        fYgPowerLast = m_fYgPower;
        if (m_fYgPower < fYgLast)
        {
            fYgLast = m_fYgPower;
        }
    }

    // 需量
    float fa = m_XLToday - m_XLmargin;    
    if (fa < 0) fa = 0;    
    // 需量 - 并网表
    float fb = fa - m_fXLYgPower;
    
    // 超过了需量
    if (fb < 0)
    {
        if (XLDownDelay.secsTo(QDateTime::currentDateTime()) > 2)
        {
            XLDownDelay = QDateTime::currentDateTime();
            fYg = fYgLast - 2*fabs(fb);
        }
    }
    else if (fb >= 10)
    {
        if (XLUpDelay.secsTo(QDateTime::currentDateTime()) > 2)
        {
            XLUpDelay = QDateTime::currentDateTime();
            if (fb >= 100)
            {
                fYg = fYgLast + 50;
            }
            else if (fb >= 50)
            {
                fYg = fYgLast + 25;
            }
            else
            {
                fYg = fYgLast + 5;
            }
        }
    }
    else
    {
        fYg = fYgLast;
    }

    if (fYg < 0)
    {
        fYg = 0;
    }

    if (fYg > fYgPowerLast)
    {
        fYg = fYgPowerLast;
    }    

    fYgLast = fYg;

    return fYg;
}

float StrategyThread::NLFunc0(float &fYgLast)
{
    static float fYg = 0, fYgPowerLast = 0;

    if (fYgPowerLast != m_fYgPower)
    {
        fYgPowerLast = m_fYgPower;
        if (m_fYgPower > fYgLast)
        {
            fYgLast = m_fYgPower;
        }
    }

    if (m_fNLYgPower[0] < m_NLPoint)
    {
        if (NLDownDelay[0].secsTo(QDateTime::currentDateTime()) > 2)
        {
            NLDownDelay[0] = QDateTime::currentDateTime();
            fYg = fYgLast - (m_fNLYgPower[0]-m_NLPoint) + m_NLmargin;
        }
    }
    else if (m_fNLYgPower[0] >= (10 + m_NLPoint))
    {
        if (NLUpDelay.secsTo(QDateTime::currentDateTime()) > 2)
        {
            NLUpDelay = QDateTime::currentDateTime();
            if (m_fNLYgPower[0] >= (100 + m_NLPoint))
            {
                fYg = fYgLast - 50;
            }
            else if (m_fNLYgPower[0] >= (50 + m_NLPoint))
            {
                fYg = fYgLast - 25;
            }
            else
            {
                fYg = fYgLast - 5;
            }
        }
    }
    else
    {
        fYg = fYgLast;
    }

    if (fYg > 0)
    {
        fYg = 0;
    }

    if (fYg < fYgPowerLast)
    {
        fYg = fYgPowerLast;
    }

    fYgLast = fYg;

    return fYg;
}

float StrategyThread::NLFunc1(float &fYgLast)
{
    static float fYg = 0, fYgPowerLast = 0;

    if (fYgPowerLast != m_fYgPower)
    {
        fYgPowerLast = m_fYgPower;
        if (m_fYgPower > fYgLast)
        {
            fYgLast = m_fYgPower;
        }
    }

    if (m_fNLYgPower[0] < m_NLPoint)
    {
        if (NLDownDelay[0].secsTo(QDateTime::currentDateTime()) > 2)
        {
            NLDownDelay[0] = QDateTime::currentDateTime();
            fYg = fYgLast - (m_fNLYgPower[0]-m_NLPoint) + m_NLmargin;
        }
    }
    else if (m_fNLYgPower[1] < (m_NLPoint/3))
    {
        if (NLDownDelay[1].secsTo(QDateTime::currentDateTime()) > 2)
        {
            NLDownDelay[1] = QDateTime::currentDateTime();
            fYg = fYgLast - ((m_fNLYgPower[1]-m_NLPoint/3)*3) + m_NLmargin;
        }
    }
    else if ((m_fNLYgPower[0] >= (10 + m_NLPoint)) && (m_fNLYgPower[1] >= ((10 + m_NLPoint)/3)))
    {
        if (NLUpDelay.secsTo(QDateTime::currentDateTime()) > 2)
        {
            NLUpDelay = QDateTime::currentDateTime();
            if ((m_fNLYgPower[0] >= (100 + m_NLPoint)) && (m_fNLYgPower[1] >= ((100 + m_NLPoint)/3)))
            {
                fYg = fYgLast - 50;
            }
            else if ((m_fNLYgPower[0] >= (50 + m_NLPoint)) && (m_fNLYgPower[1] >= ((50 + m_NLPoint)/3)))
            {
                fYg = fYgLast - 25;
            }
            else
            {
                fYg = fYgLast - 5;
            }
        }
    }
    else
    {
        fYg = fYgLast;
    }

    if (fYg > 0)
    {
        fYg = 0;
    }

    if (fYg < fYgPowerLast)
    {
        fYg = fYgPowerLast;
    }

    fYgLast = fYg;

    return fYg;
}

void StrategyThread::initializeRangStruct(void)
{
	for (int i = 0; i < KG_MAX_TIME_NUMBER; i++)
	{
		TimeSegment& ts = m_timeSegment[i];
		ts.nStartMinute = i * 15;
		ts.nEndMinute = (i + 1) * 15;
	}
}

void StrategyThread::getLocalCurveYgPower(void)
{
    if(!KgGlobalData::m_dataBase.isOpen())
    {
        KgGlobalData::m_dataBase = QSqlDatabase::addDatabase("QSQLITE");
        KgGlobalData::m_dataBase.setDatabaseName("../db/bams.db");
        KgGlobalData::m_dataBase.open();
    }      
	int i = 0;  
	QString strSql;
	QSqlQuery query(KgGlobalData::m_dataBase); 
	strSql = QString("select power from tblcurvecfg where curveType = '1' ORDER BY id asc;");
	if (!query.exec(strSql))
	{
        KG_LOG_INFO(QString("读取就地曲线-连接数据库表失败"));
		return;
	}
	while (query.next())
	{	
		if (i < KG_MAX_TIME_NUMBER)
		{
			m_LocalCurveYgPower[i] = query.value(0).toFloat();
		}
		else
		{
			break;
		}
		i++;
	}
}

void StrategyThread::getLocalCurveWgPower(void)
{
	int i = 0;       
	QString strSql;
	QSqlQuery query(KgGlobalData::m_dataBase); 
	strSql = QString("select wgPower from tblcurvecfg where curveType = '1' ORDER BY id asc;");
	if (!query.exec(strSql))
	{
		KG_LOG_INFO(QString("读取就地曲线-连接数据库表失败"));
		return;
	}
	while (query.next())
	{	
		if (i < KG_MAX_TIME_NUMBER)
		{
			m_LocalCurveWgPower[i] = query.value(0).toFloat();
		}
		else
		{
			break;
		}
		i++;
	}
}

void StrategyThread::getRemoteCurveYgPower(void)
{
	int i = 0;   
	QString strSql;
	QSqlQuery query(KgGlobalData::m_dataBase); 
	strSql = QString("select power from tblcurvecfg where curveType = '2' ORDER BY id asc;");
	if (!query.exec(strSql))
	{
        KG_LOG_INFO(QString("读取远程曲线-连接数据库表失败"));
		return;
	}
	while (query.next())
	{	
		if (i < KG_MAX_TIME_NUMBER)
		{
			m_RemoteCurveYgPower[i] = query.value(0).toFloat();
		}
		else
		{
			break;
		}
		i++;
	}
}

void StrategyThread::getRemoteCurveWgPower(void)
{
	int i = 0;    
	QString strSql;
	QSqlQuery query(KgGlobalData::m_dataBase); 
	strSql = QString("select wgPower from tblcurvecfg where curveType = '2' ORDER BY id asc;");
	if (!query.exec(strSql))
	{
		KG_LOG_INFO(QString("读取远程曲线-连接数据库表失败"));
		return;
	}
	while (query.next())
	{	
		if (i < KG_MAX_TIME_NUMBER)
		{
			m_RemoteCurveWgPower[i] = query.value(0).toFloat();
		}
		else
		{
			break;
		}
		i++;
	}
}

void StrategyThread::getXLNL(void)
{
	int i = 0;     
	QString strSql;
	QSqlQuery query(KgGlobalData::m_dataBase); 
	strSql = QString("SELECT value FROM tblSysParam WHERE ( \
	code = 'stackTempParam11' OR code = 'stackTempParam12' \
	OR code = 'stackTempParam13' OR code = 'stackTempParam14' \
	OR code = 'stackTempParam15' OR code = 'stackTempParam16' \
	OR code = 'stackTempParam17' OR code = 'stackTempParam18' \
	OR code = 'stackTempParam19' OR code = 'stackTempParam20' \
	OR code = 'stackTempParam21' OR code = 'stackTempParam22' \
	OR code = 'stackTempParam23' OR code = 'stackTempParam24' \
	OR code = 'stackTempParam25' OR code = 'stackTempParam26' \
	OR code = 'bankMaxChargePower' OR code = 'bankMaxDischargePower')");    
	if (!query.exec(strSql))
	{
		KG_LOG_INFO(QString("读取需量逆流-连接数据库表失败"));
		return;
	}
	while (query.next())
	{	
		if (i < 12)
		{
			m_XLmonth[i] = query.value(0).toFloat();
		}
        else if (i == 12)
        {
            m_NLmargin = query.value(0).toFloat();
        }
        else if (i == 13)
        {
            m_XLmargin = query.value(0).toFloat();
        }
        else if (i == 14)
        {
            m_bNL = query.value(0).toFloat();
        }
        else if (i == 15)
        {
            m_bXL = query.value(0).toFloat();
        }
		else
		{
			break;
		}
        i++;
	}
	QDate currentDate = QDate::currentDate();
	int month = currentDate.month();   
	m_XLToday = m_XLmonth[month-1];  // 本月需量	
}
